﻿namespace WebUI.Controls
{
    using AjaxControlToolkit;
    //using MgExtensions.HtzCore.Features;
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using NegevGas.Core;
    using System.Linq;
    public class EditPropertyFieldTemplate : ITemplate
    {
        private List<Control> c;

        public EditPropertyFieldTemplate(FeatureProperty p) : this(p, string.Empty)
        {
        }

        public EditPropertyFieldTemplate(FeatureProperty p, string sdfFile)
        {
            this.c = new List<Control>();
            if (p.IsReadOnly)
            {
                Label  lbl = new Label {
                    Text = (p.Value != null) ? p.Value.ToString() : string.Empty,
                    ID = p.Name
                };
                
                this.c.Add(lbl);
            }
            else
            {
                DropDownList ddl;
                LiteralControl lnkEditConstraint;
                if (p.AviableValues != null)
                {
                    ddl = new DropDownList {
                        ID = p.Name
                    };
                  
                    foreach (string t in from x in p.AviableValues
                        orderby x
                        select x)
                    {
                        ddl.Items.Add(new ListItem(t));
                    }
                    ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText(p.Value.ToString()));
                    ddl.Style["width"] = "150px";
                    this.c.Add(ddl);
                    if (!string.IsNullOrEmpty(sdfFile))
                    {
                       lnkEditConstraint = new LiteralControl {
                            Text = string.Format("<span onclick='ShowSdfEditorDialog(\"{0}\", \"_FeatureCard_{0}\")' style='cursor: pointer;'><img src='{1}' alt='+' width='16' height='16'></span>", HttpUtility.UrlEncode(p.Name), VirtualPathUtility.ToAbsolute("~/Images/Edit.png"))
                        };
                       
                        ddl.Style["width"] = "115px";
                        this.c.Add(lnkEditConstraint);
                    }
                }
                else if ((p.LookUpValues != null) && (p.LookUpValues.Count != 0))
                {
                  ddl = new DropDownList {
                        ID = p.Name
                    };
                  
                    foreach (KeyValuePair<int, string> item in from x in p.LookUpValues
                        where x.Key != -1
                        select x)
                    {
                        ddl.Items.Add(new ListItem(item.Value, item.Key.ToString()));
                    }
                    ddl.SelectedIndex = (p.Value == null) ? 0 : ddl.Items.IndexOf(ddl.Items.FindByValue(p.Value.ToString()));
                    ddl.Style["width"] = "150px";
                    this.c.Add(ddl);
                    if (!string.IsNullOrEmpty(sdfFile))
                    {
                       lnkEditConstraint = new LiteralControl {
                            Text = string.Format("<span onclick='ShowSdfEditorDialog(\"{0}\", \"_FeatureCard_{0}\")' style='cursor: pointer;'><img src='{1}' alt='+' width='16' height='16'></span></span>", HttpUtility.UrlEncode(p.Name), VirtualPathUtility.ToAbsolute("~/Images/Edit.png"))
                        };
                        
                        ddl.Style["width"] = "115px";
                        this.c.Add(lnkEditConstraint);
                    }
                }
                else
                {
                    TextBox tb;
                    switch (p.PropertyType)
                    {
                        case 1:
                        {
                            CheckBox cb = new CheckBox {
                                ID = p.Name
                            };
                          
                            if (p.Value == null)
                            {
                                p.Value = "False";
                            }
                            cb.Checked = bool.Parse(p.Value.ToString());
                            this.c.Add(cb);
                            return;
                        }
                        case 3:
                        {
                             tb = new TextBox {
                                ID = p.Name,
                                ToolTip = "dd/MM/yyyy"
                            };
                           
                            if (p.Value != null)
                            {
                                tb.Text = p.DisplayedValue;
                            }
                            tb.Style.Add("direction", "ltr");
                            tb.Style.Add("margin-left", "2px");
                            tb.Style.Add("text-align", "right");
                            tb.Style.Add("max-width", "95px");
                            this.c.Add(tb);
                            if ((p.Alias == "תאריך עדכון") || (p.Alias == "עדכון אחרון"))
                            {
                                tb.Enabled = false;
                            }
                            else
                            {
                                tb.CssClass = "dateField";
                                ImageButton btnPicker = new ImageButton
                                {
                                    ID = string.Format("{0}_btnPicker", p.Name),
                                    ImageUrl = "~/Images/calendar.jpg",
                                    ImageAlign = ImageAlign.Middle,
                                    Height = new Unit("16px")
                                };
                               
                                this.c.Add(btnPicker);
                                CalendarExtender ext = new CalendarExtender {
                                    PopupButtonID = btnPicker.ID,
                                    TargetControlID = tb.ID,
                                    Format = "dd/MM/yyyy",
                                    CssClass = "cal_Theme1",
                                    ID = string.Format("{0}_ext", p.Name),
                                    Enabled = true
                                };
                              
                                this.c.Add(ext);
                                MaskedEditExtender meExt = new MaskedEditExtender {
                                    Mask = "99/99/9999",
                                    InputDirection = MaskedEditInputDirection.RightToLeft,
                                    TargetControlID = tb.ID,
                                    MaskType = MaskedEditType.Date,
                                    ID = string.Format("{0}_MaskEditExt", p.Name)
                                };
                               
                                this.c.Add(meExt);
                            }
                            return;
                        }
                    }
                     tb = new TextBox {
                        ID = p.Name
                    };
                   
                    tb.Style.Add(HtmlTextWriterStyle.Width, "100%");
                    tb.Text = (p.Value != null) ? p.Value.ToString() : string.Empty;
                    this.c.Add(tb);
                }
            }
        }

        public void InstantiateIn(Control container)
        {
            foreach (Control ctrl in this.c)
            {
                container.Controls.Add(ctrl);
            }
        }
    }
}

